

package test.uis.buttons;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.helpers.FontHelper;
import com.megacrit.cardcrawl.helpers.Hitbox;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.helpers.MathHelper;
import com.megacrit.cardcrawl.helpers.controller.CInputActionSet;
import com.megacrit.cardcrawl.helpers.input.InputHelper;
import com.megacrit.cardcrawl.localization.UIStrings;

public class CustomButton {
    private static final UIStrings uiStrings;
    public static final String[] TEXT;
    private static final int W = 512;
    private static final int H = 256;
    private static final float TAKE_Y;
    private static final float SHOW_X;
    private static final float HIDE_X;
    private float current_x;
    private float target_x;
    private boolean isHidden;
    public boolean isDisabled;
    private Color textColor;
    private Color btnColor;
    private float target_a;
    public String buttonText;
    private static final float HITBOX_W;
    private static final float HITBOX_H;
    public Hitbox hb;

    public CustomButton() {
        this.current_x = HIDE_X;
        this.target_x = this.current_x;
        this.isHidden = true;
        this.isDisabled = true;
        this.textColor = Color.WHITE.cpy();
        this.btnColor = Color.WHITE.cpy();
        this.target_a = 0.0F;
        this.hb = new Hitbox(0.0F, 0.0F, HITBOX_W, HITBOX_H);
        this.buttonText = TEXT[0];
        this.hb.move((float)Settings.WIDTH / 2.0F, TAKE_Y);
    }

    public void update() {
        if (!this.isHidden) {
            this.hb.update();
        }

        if (!this.isDisabled) {
            if (this.hb.justHovered) {
                CardCrawlGame.sound.play("UI_HOVER");
            }

            if (this.hb.hovered && InputHelper.justClickedLeft) {
                this.hb.clickStarted = true;
                CardCrawlGame.sound.play("UI_CLICK_1");
            }
        }

        if (this.current_x != this.target_x) {
            this.current_x = MathUtils.lerp(this.current_x, this.target_x, Gdx.graphics.getDeltaTime() * 9.0F);
            if (Math.abs(this.current_x - this.target_x) < Settings.UI_SNAP_THRESHOLD) {
                this.current_x = this.target_x;
            }
        }

        this.textColor.a = MathHelper.fadeLerpSnap(this.textColor.a, this.target_a);
        this.btnColor.a = this.textColor.a;
    }

    public void hideInstantly() {
        this.current_x = HIDE_X;
        this.target_x = HIDE_X;
        this.isHidden = true;
        this.target_a = 0.0F;
        this.textColor.a = 0.0F;
    }

    public void hide() {
        if (!this.isHidden) {
            this.target_a = 0.0F;
            this.target_x = HIDE_X;
            this.isHidden = true;
        }

    }

    public void show() {
        if (this.isHidden) {
            this.textColor.a = 0.0F;
            this.target_a = 1.0F;
            this.target_x = SHOW_X;
            this.isHidden = false;
        }

    }

    public void disable() {
        if (!this.isDisabled) {
            this.hb.hovered = false;
            this.isDisabled = true;
            this.btnColor = Color.GRAY.cpy();
            this.textColor = Color.LIGHT_GRAY.cpy();
        }

    }

    public void enable() {
        if (this.isDisabled) {
            this.isDisabled = false;
            this.btnColor = Color.WHITE.cpy();
            this.textColor = Settings.CREAM_COLOR.cpy();
        }

    }

    public void render(SpriteBatch sb) {
        sb.setColor(this.btnColor);
        this.renderButton(sb);
        if (this.hb.hovered && !this.isDisabled && !this.hb.clickStarted) {
            sb.setBlendFunction(770, 1);
            sb.setColor(new Color(1.0F, 1.0F, 1.0F, 0.3F));
            this.renderButton(sb);
            sb.setBlendFunction(770, 771);
        }

        if (!this.isHidden) {
            // 修改：文本位置基于 hb 的中心
            FontHelper.renderFontCentered(sb, FontHelper.buttonLabelFont, this.buttonText, this.hb.cX, this.hb.cY, this.textColor);
        }
    }

    private void renderButton(SpriteBatch sb) {
        if (!this.isHidden) {
            float x = this.hb.cX - 256.0F; // 修改：基于 hb.cX
            float y = this.hb.cY - 128.0F; // 修改：基于 hb.cY

            if (this.isDisabled) {
                sb.draw(ImageMaster.REWARD_SCREEN_TAKE_USED_BUTTON, x, y, 256.0F, 128.0F, 512.0F, 256.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 512, 256, false, false);
            } else {
                if (this.hb.clickStarted) {
                    sb.setColor(Color.LIGHT_GRAY);
                }
                sb.draw(ImageMaster.REWARD_SCREEN_TAKE_BUTTON, x, y, 256.0F, 128.0F, 512.0F, 256.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 512, 256, false, false);
            }

            if (Settings.isControllerMode) {
                sb.draw(CInputActionSet.proceed.getKeyImg(), this.hb.cX - 32.0F - 100.0F * Settings.scale, this.hb.cY - 32.0F + 2.0F * Settings.scale, 32.0F, 32.0F, 64.0F, 64.0F, Settings.scale, Settings.scale, 0.0F, 0, 0, 64, 64, false, false);
            }

            this.hb.render(sb);
        }
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString("Confirm Button");
        TEXT = uiStrings.TEXT;
        TAKE_Y = 475.0F * Settings.scale;
        SHOW_X = (float)Settings.WIDTH - 256.0F * Settings.scale;
        HIDE_X = SHOW_X + 400.0F * Settings.scale;
        HITBOX_W = 260.0F * Settings.scale;
        HITBOX_H = 80.0F * Settings.scale;
    }
}
